package com.turnengine.client.local.turn.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.local.turn.bean.ITurnSummary;
import com.turnengine.client.local.turn.command.ExecuteTurns;
import com.turnengine.client.local.turn.command.GetNextTurnUpdateTime;
import com.turnengine.client.local.turn.command.GetTurnInterval;
import com.turnengine.client.local.turn.command.GetTurnNumber;
import com.turnengine.client.local.turn.command.GetTurnSummary;
import com.turnengine.client.local.turn.command.GetTurnUpdating;
import com.turnengine.client.local.turn.command.IExecuteTurns;
import com.turnengine.client.local.turn.command.IGetNextTurnUpdateTime;
import com.turnengine.client.local.turn.command.IGetTurnInterval;
import com.turnengine.client.local.turn.command.IGetTurnNumber;
import com.turnengine.client.local.turn.command.IGetTurnSummary;
import com.turnengine.client.local.turn.command.IGetTurnUpdating;
import com.turnengine.client.local.turn.command.ISetTurnInterval;
import com.turnengine.client.local.turn.command.ISetTurnsEnabled;
import com.turnengine.client.local.turn.command.SetTurnInterval;
import com.turnengine.client.local.turn.command.SetTurnsEnabled;
import com.turnengine.client.local.turn.enums.TurnUpdating;
import com.turnengine.client.local.turn.enums.TurnsEnabled;

/**
 * The Local Turn Command Set.
 */
public class LocalTurnCommandSet implements ILocalTurnCommandSet {

	@Override
	public int getTurnNumber(long loginId, int instanceId) {
		IGetTurnNumber command = new GetTurnNumber(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int executeTurns(long loginId, int instanceId, int turns) {
		IExecuteTurns command = new ExecuteTurns(loginId, instanceId, turns);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public long setTurnInterval(long loginId, int instanceId, long interval) {
		ISetTurnInterval command = new SetTurnInterval(loginId, instanceId, interval);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Long> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public long getTurnInterval(long loginId, int instanceId) {
		IGetTurnInterval command = new GetTurnInterval(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Long> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public TurnsEnabled setTurnsEnabled(long loginId, int instanceId, TurnsEnabled updating) {
		ISetTurnsEnabled command = new SetTurnsEnabled(loginId, instanceId, updating);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<TurnsEnabled> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public TurnUpdating getTurnUpdating(long loginId, int instanceId) {
		IGetTurnUpdating command = new GetTurnUpdating(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<TurnUpdating> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public long getNextTurnUpdateTime(long loginId, int instanceId) {
		IGetNextTurnUpdateTime command = new GetNextTurnUpdateTime(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Long> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public ITurnSummary getTurnSummary(long loginId, int instanceId) {
		IGetTurnSummary command = new GetTurnSummary(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<ITurnSummary> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
